package com.sinovatech.ologger.actionlog.dao;

import com.sinovatech.ologger.actionlog.aop.annotation.PassLogMethod;
import com.sinovatech.ologger.actionlog.entity.OperationLog;
import com.sinovatech.ologger.actionlog.vo.OperationLogVO;
import com.sinovatech.ologger.core.dao.extend.HibernateEntityExtendDao;
import com.sinovatech.ologger.core.dao.support.Page;
import com.sinovatech.ologger.util.StringHelper;

public class OperationLogDao extends HibernateEntityExtendDao<OperationLog> {

	private final static String HQL_COMMON = " from OperationLog log ";
	private final static String HQL_COMMON_SELECT = "select distinct log " + HQL_COMMON;

	@PassLogMethod
	@Override
	public void save(Object entity) {
		super.save(entity);
	}

	public Page findOperationLogPageByCondition(OperationLogVO logVO) {
		StringBuilder hql = new StringBuilder(HQL_COMMON_SELECT).append(" inner join log.permissions p where 1= 1 ");
		if (!StringHelper.isEmpty(logVO.getFromTime())) {
			hql.append(" and date_format(log.createTime,'%Y-%m-%d') >= '").append(logVO.getFromTime()).append("' ");
		}
		if (!StringHelper.isEmpty(logVO.getToTime())) {
			hql.append(" and date_format(log.createTime,'%Y-%m-%d') <= '").append(logVO.getToTime()).append("' ");
		}
		hql.append(" order by log.createTime desc ");
		return this.pagedQuery(hql.toString(), logVO.getPageNo(), logVO.getPageSize());
	}
}
